﻿using Bussiness.DEV;
using Model;
using System;
using System.Collections.Generic;

namespace Bussiness
{
    public class FANUC : IDEV
    {
        public Device Device { get; set; }
        public List<DevicePOI> DevicePOIList { get; set; }
        private ushort _FlibHnd = 0;
        public Result Connect()
        {
            Result result = new Result();
            ushort _port = Convert.ToUInt16(Device.Port);
            short resultCode = Focas1.cnc_allclibhndl3(Device.IP, _port, 3, out _FlibHnd);
            if(resultCode== Focas1.EW_OK)
            {
                result.Success = true;
            }
            else
            {
                result.Success = false;
            }

            return result;
        }

        public Result<List<Cfg_Field>> Read()
        {
            Result<List<Cfg_Field>> result = new Result<List<Cfg_Field>>();
            result.Data = new List<Cfg_Field>();
            Focas1.ODBPOS odbpos = new Focas1.ODBPOS();
            short num = Focas1.MAX_AXIS;
            short type = -1;
            short ret = Focas1.cnc_rdposition(_FlibHnd, type, ref num, odbpos);
            if (ret == 0)
            {
                result.Success = true;
            }

            Focas1.ODBSYS odbsys = new Focas1.ODBSYS();
            short ret2 = Focas1.cnc_sysinfo(_FlibHnd, odbsys);
            if (ret2 == 0)
            {
                result.Success = true;
            }

            Focas1.ODBACT odbact = new Focas1.ODBACT();
            Focas1.ODBST odbst = new Focas1.ODBST();
            short ret3 = Focas1.cnc_statinfo(_FlibHnd, odbst);
            if (ret3 == 0)
            {
                result.Success = true;
            }

            Focas1.ODBPRO odbpro = new Focas1.ODBPRO();
            short ret4 = Focas1.cnc_rdprgnum(_FlibHnd, odbpro);
            if (ret4 == 0)
            {
                result.Success = true;
            }

            Focas1.IODBPSD_1 iodbpsd_1 = new Focas1.IODBPSD_1();
            short ret5;
            foreach (var devicePOI in DevicePOIList)
            {
                Cfg_Field cfg_Field = new Cfg_Field();
                cfg_Field.name = devicePOI.DevPOIAlias;
                bool has = true;
                switch (devicePOI.DevPOIName)
                {
                    case "X轴绝对坐标":
                        if (ret == 0)
                        {
                            cfg_Field.value = (odbpos.p1.abs.data * Math.Pow(10, -odbpos.p1.abs.dec)).ToString();
                        }
                        
                        break;
                    case "Y轴绝对坐标":
                        if (ret == 0)
                        {
                            cfg_Field.value = (odbpos.p2.abs.data * Math.Pow(10, -odbpos.p2.abs.dec)).ToString();
                        }
                        
                        break;
                    case "X轴相对坐标":
                        if (ret == 0)
                        {
                            cfg_Field.value = (odbpos.p1.rel.data * Math.Pow(10, -odbpos.p1.rel.dec)).ToString();
                        }
                        
                        break;
                    case "Y轴相对坐标":
                        if (ret == 0)
                        {
                            cfg_Field.value = (odbpos.p2.rel.data * Math.Pow(10, -odbpos.p2.rel.dec)).ToString();
                        }
                        
                        break;
                    case "主轴转速":
                        ret = Focas1.cnc_acts(_FlibHnd, odbact);
                        if (ret == 0)
                        {
                            result.Success = true;
                            cfg_Field.value= odbact.data.ToString()+ "mm/min";
                        }

                        break;
                    case "最大轴数":
                        if (ret2 == 0)
                        {
                            cfg_Field.value = odbsys.max_axis.ToString();
                        }
                        
                        break;
                    case "CNC工作模式":
                        if (ret3 == 0)
                        {
                            switch (odbst.aut)
                            {
                                case 0:
                                    cfg_Field.value = "MDI";
                                    break;
                                case 1:
                                    cfg_Field.value = "MEMory";
                                    break;
                                case 2:
                                    cfg_Field.value = "Not Defined";
                                    break;
                                case 3:
                                    cfg_Field.value = "EDIT";
                                    break;
                                case 4:
                                    cfg_Field.value = "HaNDle";
                                    break;
                                case 5:
                                    cfg_Field.value = "JOG";
                                    break;
                                case 6:
                                    cfg_Field.value = "Teach in JOG";
                                    break;
                                case 7:
                                    cfg_Field.value = "Teach in HaNDle";
                                    break;
                                case 8:
                                    cfg_Field.value = "INC·feed";
                                    break;
                                case 9:
                                    cfg_Field.value = "REFerence";
                                    break;
                                case 10:
                                    cfg_Field.value = "ReMoTe";
                                    break;
                                default:
                                    cfg_Field.value = "others mode";
                                    break;
                            }
                        }
                        
                        break;
                    case "主程序号":
                        if (ret4 == 0)
                        {
                            cfg_Field.value = odbpro.mdata.ToString();
                        }

                        break;
                    case "当前程序号":
                        if (ret4 == 0)
                        {
                            cfg_Field.value = odbpro.data.ToString();
                        }

                        break;
                    case "开机时间":
                        ret5 = Focas1.cnc_rdparam(_FlibHnd, 6750, 0, 8 + 32, iodbpsd_1);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            cfg_Field.value = (iodbpsd_1.ldata * 60).ToString();
                        }

                        break;
                    case "运行时间":
                        ret5 = Focas1.cnc_rdparam(_FlibHnd, 6751, 0, 8, iodbpsd_1);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            int workingTimeSec = iodbpsd_1.ldata / 1000;
                            ret5 = Focas1.cnc_rdparam(_FlibHnd, 6752, 0, 8, iodbpsd_1);
                            if (ret5 == 0)
                            {
                                result.Success = true;
                                int workingTimeMin = iodbpsd_1.ldata;
                                int cycSec = workingTimeMin * 60 + workingTimeSec;
                                cfg_Field.value = cycSec.ToString();
                            }
                        }
                        break;
                    case "CNC型号":
                        if (ret3 == 0)
                        {
                            cfg_Field.value = odbsys.series[0].ToString() + odbsys.series[1].ToString() + odbsys.series[2].ToString() + odbsys.series[3].ToString();
                        }
                        break;
                    case "CNC类型":
                        if (ret3 == 0)
                        {
                            string cnctype = odbsys.cnc_type[0].ToString() + odbsys.cnc_type[1].ToString();
                            switch (cnctype)
                            {
                                case "15":
                                    cfg_Field.value = "Series 15/15i";
                                    break;
                                case "16":
                                    cfg_Field.value = "Series 16/16i";
                                    break;
                                case "18":
                                    cfg_Field.value = "Series 18/18i";
                                    break;
                                case "21":
                                    cfg_Field.value = "Series 21/21i";
                                    break;
                                case "30":
                                    cfg_Field.value = "Series 30i";
                                    break;
                                case "31":
                                    cfg_Field.value = "Series 31i";
                                    break;
                                case "32":
                                    cfg_Field.value = "Series 32i";
                                    break;
                                case "35":
                                    cfg_Field.value = "Series 35i";
                                    break;
                                case " 0":
                                    cfg_Field.value = "Series 0i";
                                    break;
                                case "PD":
                                    cfg_Field.value = "Power Mate i-D";
                                    break;
                                case "PH":
                                    cfg_Field.value = "Power Mate i-H";
                                    break;
                                case "PM":
                                    cfg_Field.value = "Power Motion i";
                                    break;
                                default:
                                    cfg_Field.value = "其它类型";
                                    break;
                            }
                        }
                        
                        break;
                    case "设备状态":
                        if (ret3 == 0)
                        {
                            if (odbst.alarm == 0)
                            {
                                if (odbst.run == 3)
                                {
                                    cfg_Field.value = "运行";
                                }
                                else
                                {
                                    cfg_Field.value = "待机";
                                }
                            }
                            else
                            {
                                cfg_Field.value = "报警";
                            }
                        }

                        break;
                    case "进给倍率":
                        Focas1.IODBPMC0 ig = new Focas1.IODBPMC0();
                        ret5 = Focas1.pmc_rdpmcrng(_FlibHnd, 0, 1, 12, 13, 8 + 1 * 2, ig);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            cfg_Field.value = (100 - (ig.cdata[0] - 155)).ToString();
                        }

                        break;
                    case "单次加工数":
                        Focas1.ODBM odbm = new Focas1.ODBM();
                        ret5 = Focas1.cnc_rdmacro(_FlibHnd, 0xf3d, 0x0a, odbm);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            cfg_Field.value = (odbm.mcr_val / 100000).ToString();
                        }

                        break;
                    case "工件总数":
                        ret5 = Focas1.cnc_rdparam(_FlibHnd, 6712, 0, 8, iodbpsd_1);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            cfg_Field.value = iodbpsd_1.ldata.ToString();
                        }

                        break;
                    case "切削时间":
                        ret5 = Focas1.cnc_rdparam(_FlibHnd, 6753, 0, 8 + 32, iodbpsd_1);
                        if (ret5 == Focas1.EW_OK)
                        {
                            result.Success = true;
                            int cuttingTimeSec = iodbpsd_1.ldata / 1000;
                            ret = Focas1.cnc_rdparam(_FlibHnd, 6754, 0, 8 + 32, iodbpsd_1);
                            if (ret == Focas1.EW_OK)
                            {
                                result.Success = true;
                                int cuttingTimeMin = iodbpsd_1.ldata;
                                cfg_Field.value = (cuttingTimeMin * 60 + cuttingTimeSec).ToString();
                            }
                        }

                        break;
                    case "报警信息":
                        int almdsta;
                        ret5 = Focas1.cnc_alarm2(_FlibHnd, out almdsta);
                        if (ret5 == 0)
                        {
                            result.Success = true;
                            switch (almdsta)
                            {
                                case 0:
                                    cfg_Field.value = "参数开启（SW）";
                                    break;
                                case 1:
                                    cfg_Field.value = "关机参数设置（PW）";
                                    break;
                                case 2:
                                    cfg_Field.value = "I / O错误（IO）";
                                    break;
                                case 3:
                                    cfg_Field.value = "前景P / S（PS";
                                    break;
                                case 4:
                                    cfg_Field.value = "超程，外部数据（OT";
                                    break;
                                case 5:
                                    cfg_Field.value = "过热报警（OH）";
                                    break;
                                case 6:
                                    cfg_Field.value = "伺服报警（SV";
                                    break;
                                case 7:
                                    cfg_Field.value = "数据I / O错误（SR）";
                                    break;
                                case 8:
                                    cfg_Field.value = "宏指令报警（MC";
                                    break;
                                case 9:
                                    cfg_Field.value = "主轴报警（SP）";
                                    break;
                                case 10:
                                    cfg_Field.value = "其他警报（DS）";
                                    break;
                                case 11:
                                    cfg_Field.value = "有关故障防止功能（IE）的警报";
                                    break;
                                case 12:
                                    cfg_Field.value = "背景P / S（BG）";
                                    break;
                                case 13:
                                    cfg_Field.value = "同步错误（SN）";
                                    break;
                                case 14:
                                    cfg_Field.value = "保留";
                                    break;
                                case 15:
                                    cfg_Field.value = "外部报警信息（EX）";
                                    break;
                                case 16:
                                    cfg_Field.value = "正向超程（软限位1）";
                                    break;
                                default:
                                    cfg_Field.value = "未知错误";
                                    break;
                            }
                        }
                            
                        break;
                    default:
                        has=false;
                        break;
                }

                if (has)
                {
                    result.Data.Add(cfg_Field);
                }
            }

            return result;
        }
    }
}
